<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
class RoomguanliController extends AdminbaseController{
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("Room");
		$this->log = D("RoomLog");



		$this->assign("building",$this->user["building"]);




		$this->assign("buildings",$this->buildings);





	}


	function today_checkout(){


//		dump($this->buildings);

		foreach($this->buildings as $k=>$v){

			$s[]=$k;

		}


		$map["building"]=array("in",$s);
		$map["checkout"]=array("between",array(date('Y-m-d'),date('Y-m-d',strtotime('+1 day'))));



//		$map['_string'] = "checkout > '".date('Y-m-d')."' AND  checkout < '".date('Y-m-d',strtotime('+1 day'))."'";


//

//		$where["checkout"]=array("neq","");
//
//		$map['_complex'] = $where;
//
//		$data["status"]=1;
//
//
		$list=D("RoomLog")->where($map)->select();


//		echo M()->getLastSql();

		$this->assign("list",$list);
		$this->assign("today_checkout_num",count($list));
		$this->display("Roomguanli/today_checkout");




	}
	
	function index(){


		cookie("jump_self",get_now_url());
		$building_id=I("get.building_id");

		$type=I("get.type")?I("get.type"):"status";
		$this->assign("type",$type);



		$c_building=cookie( CONTROLLER_NAME."_building");;

		if($building_id){


			cookie( CONTROLLER_NAME."_building",$building_id);


		}elseif($c_building){


			$building_id=$c_building;

		}else{
			$building_id=current($this->buildings)["id"];


		}
		$this->assign("building_id",$building_id);
//		echo $building_id;





		//总房间数
		$sum["room"]=$this->db->where(array("building"=>$building_id))->count();
		//可住总人数
		$sum["max_num"]=$this->db->where(array("building"=>$building_id))->sum("maxnum");
		//现住总人数
		$sum["room_user"]=$this->log->where(array("building"=>$building_id,"status"=>0))->count();
		//空闲房间
		$sum["kong_room"]=$this->db->where(array("building"=>$building_id,"status"=>1))->count();
		//未住满房间
		$sum["weiman_room"]=$this->db->where(array("building"=>$building_id,"status"=>2))->count();
		//空余床位
		$sum["kong_chuang"]=$sum["max_num"]-$sum["room_user"];





		$this->assign("sum",$sum);



		$map["building"]=$building_id;

		$keyword=trim(I("get.keyword"));

		if($keyword){

			$where["room_name"]=array("like","%".$keyword."%");

			$where["name"]=array("like","%".$keyword."%");

			$where['_logic'] = 'or';
			$map['_complex'] = $where;




			$building_floors=$this->log->where($map)->group("floor")->getField("floor",true);



			$rooms=$this->log->where($map)->group("room_id")->getField("room_id",true);



			foreach($building_floors as $k =>$v){


				$info[$v]=$this->db->where(array("id"=>array("in",$rooms),"floor"=>$v))->select();





			}




		}
		else{


			$building_floors=$this->db->where($map)->group("floor")->getField("floor",true);



			foreach($building_floors as $k =>$v){


				$info[$v]=$this->db->where(array("building"=>$building_id,"floor"=>$v))->select();





			}
		}



		$this->assign("info",$info);











		foreach($this->buildings as $k=>$v){

			$s[]=$k;

		}


		$sss["building"]=array("in",$s);

		$sss["checkout"]=array("between",array(date('Y-m-d'),date('Y-m-d',strtotime('+1 day'))));
//		$sss['_string'] = "checkout > '".date('Y-m-d')."' AND  checkout < '".date('Y-m-d',strtotime('+1 day'))."'";

		$today_checkout_num=D("RoomLog")->where($sss)->count();





			$this->assign("today_checkout_num",$today_checkout_num);





		$this->display("Roomguanli/index");
	}




	function tiaofang(){



		if(IS_POST){



			$old_room_id=$this->log->where(array("id"=>I("post.id")))->getField("room_id");





			$data=array(
				"id"=>I("post.id"),
				"room_id"=>I("post.room_id"),
				"building"=>F("Room")[I("post.room_id")][building],
				"floor"=>F("Room")[I("post.room_id")][floor],



			);




			if ($this->log->save($data)!==false) {
				$this->db->update_status($old_room_id);
				$this->success("调整成功！",U("room",array("room_id"=>I("post.room_id"))));
			} else {
				$this->error("调整失败！");
			}



		}else{



			$id=I("get.id");

			$rooms=D("Room")->get_rooms_select();



			$this->assign("room_id",$this->log->where(array("id"=>$id))->getField("room_id"));



			$this->assign("rooms",$rooms);
			$this->display("Roomguanli/tiaofang");



		}




	}


	function room(){




		$room_id=I("get.room_id");







		$map["room_id"]=$room_id;
		$map["status"]=0;

		$room=$this->db->where(array("id"=>$room_id))->find();





		$this->assign("room",$room);

		$data=$this->log->where($map)->select();



		$this->assign("list",$data);
		$this->assign("count",count($data));


		if(I("get.ajax")){
			$this->display("Roomguanli/room_ajax");
		}else{

			$this->display("Roomguanli/room");
		}



	}



	function checkin(){



		if(IS_POST){



			if(I("post.type")==1){

				if(!I("post.danwei")){

					$this->error("单位未选择");
				}

				if(!I("post.ldht")){
					$this->error("劳动合同号不能为空");
				}
			}
			else{

				if(!I("post.checkout")){

					$this->error("请填写离开时间");
				}

				if(!I("post.sfz")){

					$this->error("身份证未填写");
				}



			}


			if(I("post.ldht")){
				$is_ru_map["ldht"]=I("post.ldht");
			}
			if(I("post.sfz")){
				$is_ru_map["sfz"]=I("post.sfz");
			}


			$is_ru_map['_logic'] = 'or';
			$is_ru_where['_complex'] = $is_ru_map;
			$is_ru_where['status'] = 0;


			$is_ru=$this->log->where($is_ru_where)->count();

			if($is_ru){

				$this->error("已经入住");
			}

			$num=$this->log->where(array("room_id"=>I("post.room_id"),"status"=>"0"))->count();

			$room_maxnum=F("Room")[I("post.room_id")][maxnum];


			if($num>=$room_maxnum){
				$this->error("已经满员");
			}



			$room=D("Room")->where(array("id"=>I("post.room_id")))->find();



			$_POST["building"]=$room["building"];
			//$_POST["ldht"]=(string)I("post.ldht");
			$_POST["floor"]=$room["floor"];
			$_POST["room_name"]=$room["room_name"];

			$_POST["datetime"]=time();



			$_POST["checkin_admin"]=UID;

			if ($this->log->create()) {
				if ($this->log->add()!==false) {


					$this->success("操作成功！");
				} else {
					$this->error("操作失败！");
				}
			} else {
				$this->error($this->log->getError());
			}
		}else{


			$num=$this->log->where(array("room_id"=>I("get.room_id"),"status"=>"0"))->count();

			$room_maxnum= F("Room")[I("get.room_id")][maxnum];



			if($num>=$room_maxnum){
				$this->error("已经满员");
			}



			$this->display("Roomguanli/checkin");
		}
		}



	function checkout(){





		$data=array(
			"id"=>I("get.id"),
			"room_id"=>$this->log->where(array("id"=>I("get.id")))->getField("room_id"),
			"checkout"=>date("Y-m-d",time()),
			"checkout_admin"=>UID,
			"status"=>1


		);


			if ($this->log->save($data)!==false) {
				$this->success("退房成功！");
			} else {
				$this->error("退房失败！");
			}



	}

	function log(){



		$room_id=I("get.room_id");

		$this->assign("room",F("Room")[$room_id]);



		$map["room_id"]=$room_id;

		$count=$this->log->where($map)->count();
		$page = $this->page($count);
		$list=$this->log->limit($page->firstRow . ',' . $page->listRows)->order ( "checkin desc ")->where($map)->select();

		$this->assign("Page",$page->show(""));

		$this->assign("list",$list);


		if(I("get.ajax")){
			$this->display("Roomguanli/log-ajax");

		}else{
			$this->display("Roomguanli/log");

		}




	}

	
	
	}